//
//  LLT_ItemListViewController_LXM.m
//  CoderLLT
//
//  Created by 刘晓萌 on 15/12/12.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_ItemListViewController_LXM.h"
#import "LLT_ItemInfoViewController_LXM.h"

@interface LLT_ItemListViewController_LXM ()<UIWebViewDelegate,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *itemInfo;
@property (weak, nonatomic) IBOutlet UIWebView *itemCount;
@property (assign, nonatomic) NSInteger indexScrollFlag;

@property (weak, nonatomic) IBOutlet UIImageView *flagImageView;
@property (weak, nonatomic) IBOutlet UILabel *FirstTitleLable;
@property (weak, nonatomic) IBOutlet UILabel *secondTitleLable;
@property (weak, nonatomic) IBOutlet UIScrollView *playerScroll;

@end

@implementation LLT_ItemListViewController_LXM

- (void)viewDidLoad {
    [super viewDidLoad];
    self.itemInfo.delegate = self;
    self.itemCount.delegate = self;
    [self.itemInfo loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://static.lolmax.com/api/static/lol/pages/gear/gearList.html"]]];
    [self.itemCount loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://static.lolmax.com/api/static/lol/pages/gear/gearCount.html"]]];
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickLeftTitleLabelAction)];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickRightTitleLabelAction)];
    [self.FirstTitleLable addGestureRecognizer:leftTap];
    [self.secondTitleLable addGestureRecognizer:rightTap];
    
    self.FirstTitleLable.userInteractionEnabled = YES;
    self.secondTitleLable.userInteractionEnabled = YES;
    self.playerScroll.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- scrollView代理方法和 滑动点击
/*
 * scrollView滑动调用方法
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.indexScrollFlag = self.playerScroll.contentOffset.x/self.view.frame.size.width;
    //NSLog(@"这个是首页滑动的位移=%ld",self.indexScrollFlag);
    if (self.indexScrollFlag) {
        self.secondTitleLable.textColor = [UIColor whiteColor];
        self.FirstTitleLable.textColor = [UIColor grayColor];
        CGFloat x = self.secondTitleLable.center.x - (self.flagImageView.frame.size.width/2);
        [UIView animateWithDuration:0.3 animations:^{
            self.flagImageView.frame = CGRectMake(x, self.flagImageView.frame.origin.y,self.flagImageView.frame.size.width,self.flagImageView.frame.size.height);
        }];
        
    }else{
        self.secondTitleLable.textColor = [UIColor grayColor];
        self.FirstTitleLable.textColor = [UIColor whiteColor];
        CGFloat x = self.FirstTitleLable.center.x - (self.flagImageView.frame.size.width/2);
        [UIView animateWithDuration:0.3 animations:^{
            self.flagImageView.frame = CGRectMake(x, self.flagImageView.frame.origin.y,self.flagImageView.frame.size.width,self.flagImageView.frame.size.height);
        }];
    }
    
}

/**
 * 滑动点击方法
 */
-(void)didClickLeftTitleLabelAction{
    self.playerScroll.contentOffset = CGPointMake(0, self.playerScroll.contentOffset.y);
}
-(void)didClickRightTitleLabelAction{
    self.playerScroll.contentOffset = CGPointMake(self.view.frame.size.width, self.playerScroll.contentOffset.y);
}


/**
 * uiwebView按状态响应加载
 *
 * @return
 */
#pragma mark --- uiwebView 协议
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *str = [request URL].absoluteString;
    str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *strsub = [str substringToIndex:6];
    NSString *s = @"maxjia://PageConfigJson#/";
    if ([strsub isEqualToString:@"maxjia"]) {
        str =  [str stringByReplacingOccurrencesOfString:s withString:@""];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData: [str dataUsingEncoding:NSUTF8StringEncoding]
                                                            options: NSJSONReadingMutableContainers
                                                              error: nil];
        
        LLT_DetailOfArticleModel *urls = [[LLT_DetailOfArticleModel alloc]init];
        [urls setValuesForKeysWithDictionary:dic[@"result"]];
        LLT_ItemInfoViewController_LXM *infoVC = [MainStoryBoard instantiateViewControllerWithIdentifier:@"itemInfoVC"];
        infoVC.itemUrls = urls;
        [self presentViewController:infoVC animated:YES completion:nil];
    }
    
    return YES;
}

/**
 * 设置电源栏反颜色
 *
 * @return
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

/**
 * 点击左侧返回
 */
- (IBAction)didClickLeftButton:(UIButton *)sender {
    DissMissBack;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
